import Vue from 'vue'
import i18n from 'common/lang/lang.js';
Vue.filter('price', (price, fixed = 0) => {
	if (!price || isNaN(price)) {
		return 0
	}
	return fixed > 0 ? parseFloat(price).toFixed(fixed) : +parseFloat(price).toFixed(2);
})

Vue.filter('add', (rate) => {
	if (!rate || isNaN(rate)) {
		return 0
	}
	return rate > 0 ? '+' + rate : rate;
})

Vue.filter('noPassByMobile', (str) => {
	if (null != str && str != undefined && str != '') {
		var pat = /(\d{3})\d*(\d{4})/;
		return str.replace(pat, '$1****$2');
	} else {
		return "";
	}
})
Vue.filter('srcDomain', (str) => {
	if (null != str && str != undefined && str != '') {
		let ossapi = 'https://bendike.oss-cn-hangzhou.aliyuncs.com/';
		if (str.indexOf('bendike.oss') == -1) {
			return ossapi + str
		} else {
			return str
		}
	} else {
		return ''
	}

})

function checkTime(i) {
	if (i < 10) {
		i = "0" + i;
	}
	return i;
}
Vue.filter('countDown', (lastTime) => {
	var ts = lastTime; //剩余的毫秒数
	var showTime = '';
	// console.log(ts)
	if (ts > 0) {
		var dd = parseInt(ts / 60 / 60 / 24, 10); //计算剩余的天数
		var hh = parseInt(ts / 60 / 60 % 24, 10); //计算剩余的小时数
		var mm = parseInt(ts / 60 % 60, 10); //计算剩余的分钟数
		var ss = parseInt(ts % 60, 10); //计算剩余的秒数
		dd = checkTime(dd);
		hh = checkTime(hh);
		mm = checkTime(mm);
		ss = checkTime(ss);
		if (dd > 0) {
			showTime = `${dd}天:${hh}:${mm}:${ss}`
		} else {
			if (hh > 0) {
				showTime = `${hh}:${mm}:${ss}`
			} else {
				if (mm > 0) {
					showTime = `00:${mm}:${ss}`
				} else {
					if (ss > 0) {
						showTime = `00:00:${ss}`
					}
				}
			}
		}
	} else {
		showTime = 0
	}
	return showTime;
})
// 汇率的转换
Vue.filter('changeCurrency', (money = 0) => {
	let currency = uni.getStorageSync('currency')
	let rate = uni.getStorageSync('rate')
	var changemoney = ''
	if (currency == 'CNY') {
		changemoney = parseFloat(money)
	}
	if (currency == 'USD') {
		changemoney = parseFloat((money * rate).toFixed(2))
	}
	return changemoney;
})
// 根据区域  美元转化为人民币  人民币转美元
Vue.filter('changeUSD', (money = 0, area) => {
	let currency = uni.getStorageSync('currency')
	if (area == 2 && currency == 'CNY') {
		let rate = uni.getStorageSync('rate')
		var changemoney = ((money / rate).toFixed(2))
	} else if (area == 1 && currency == 'USD') {
		let rate = uni.getStorageSync('rate')
		var changemoney = ((money * rate).toFixed(2))
	} else {
		var changemoney = money
	}
	return changemoney;
})

//对万进行转换
Vue.filter('changeTH', (money = 0, standard) => {
	if (money >= standard && standard == 1000) {
		var TH = (money / standard) + '千'
	} else if (money >= standard && standard == 10000) {
		var TH = (money / standard) + i18n.t('common').万
	} else {
		var TH = money
	}
	return TH;
})
// 汉字转换
Vue.filter('changeNum', (num = 0,) => {
	var units = ['', '十', '百', '千', '万', '十万', '百万', '千万', '亿'];
	var digits = ['', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
	 
	var Cnum = String(num).replace(/./g, function(digit, index, array) {
	  return  i18n.t('common')[digits[Number(digit)] + units[array.length - index - 1]];
	});
	return Cnum;
})

